using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DealerWorld.Forms
{
    public partial class search_customer : Form
    {
        public search_customer()
        {
            InitializeComponent();

            //show all customers in the list
            Main.allCust.ForEach(delegate(Customer c) {add_lsv_row(c);});
        }

        private void add_lsv_row(Customer c)
        {
            string[] row_info = {
                c.name
                ,c.mainPhone
                ,c.vehicle.licensePlate
                ,c.vehicle.vin
            };
            ListViewItem lsv_item = new ListViewItem(row_info);
            lsv_item.Tag = c;
            lsv_customers.Items.Add(lsv_item);
        }

        private bool matchSearch(Customer c)
        {
            if (custname.Text != "")
                return c.name.ToLower().Contains(custname.Text.ToLower());
            if (custphone.Text != "")
                return c.mainPhone.Contains(custphone.Text);
            if (custvin.Text != "")
                return c.vehicle.vin.ToLower().EndsWith(custvin.Text.ToLower());
            if (custlicense.Text != "")
                return c.vehicle.licensePlate.ToLower().Contains(custlicense.Text.ToLower());
            else return false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lsv_customers.Items.Clear();

             List<Customer> matchCust = Main.allCust.FindAll(matchSearch);

            matchCust.ForEach(delegate(Customer c) { add_lsv_row(c); });
        }

        private void lsv_customers_DoubleClick(object sender, EventArgs e)
        {
            if (lsv_customers.SelectedItems.Count == 1)
            {
                Customer cust = (Customer)lsv_customers.Items[lsv_customers.SelectedIndices[0]].Tag;
                
                new Forms.customer(cust).ShowDialog();
            }
            
        }

		private void btnAddToRO_Click(object sender, EventArgs e)
		{
			repair_order.custForRO.name = lsv_customers.SelectedItems[0].SubItems[0].Text;
			repair_order.custForRO.mainPhone = lsv_customers.SelectedItems[0].SubItems[1].Text;

			this.Close();
		}

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void enter_pressed(Object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button2_Click(sender, e);
            }
        }

        private void custname_KeyUp(object sender, KeyEventArgs e)
        {
            enter_pressed(sender, e);
        }

        private void custphone_KeyUp(object sender, KeyEventArgs e)
        {
            enter_pressed(sender, e);
        }

        private void custlicense_KeyUp(object sender, KeyEventArgs e)
        {
            enter_pressed(sender, e);
        }

        private void custvin_KeyUp(object sender, KeyEventArgs e)
        {
            enter_pressed(sender, e);
        }

        private void lsv_customers_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lsv_customers_DoubleClick(sender, e);
            }
        }

        private void btn_add_cust_Click(object sender, EventArgs e)
        {
            Vehicle v = new Vehicle(null);
            Customer cust = new Customer(v);
            cust.name = custname.Text;
            cust.mainPhone = custphone.Text;
            cust.vehicle.vin = custvin.Text;
            cust.vehicle.licensePlate = custlicense.Text;

            new Forms.customer(cust).Show();
        }

    }
}